let fs = require("fs")
let jsonFormat = require("json-format")

let PLANTS = {
    suan: {
        spring: true,
        xia: true,
        qiu: true,
        winter: true,
        fat: [2, -4, 2],
        water: -1,
    },
    fanqie: {
        spring: true,
        xia: true,
        qiu: true,
        fat: [-1, -1, 2],
        water: -3,

    },
    yumi: { // 25
        spring: true,
        xia: true,
        qiu: true,
        fat: [1, 2, -1],
        water: -1,

    },
    yangcong: {
        spring: true,
        xia: true,
        qiu: true,
        fat: [-4, 2, 2],
        water: -2,

    },
    luobo: {
        spring: true,
        qiu: true,
        winter: true,
        fat: [-2, 1, 1],
        water: -1,

    },
    potato: {  // 12.5   25
        spring: true,
        qiu: true,
        winter: true,
        fat: [1, 1, -2],
        water: -1,

    },
    firelongguo: {  // 9.5  20
        spring: true,
        xia: true,
        fat: [2, 2, -4],
        water: -2,

    },
    shiliu: {
        spring: true,
        xia: true,
        fat: [-4, 2, 2],
        water: -2,

    },
    watergua: {
        spring: true,
        xia: true,
        fat: [2, -1, -1],
        water: -3,

    },
    qiezi: { // 25
        spring: true,
        qiu: true,
        fat: [1, 1, -2],
        water: -2,

    },
    luhui: {
        spring: true,
        winter: true,
        fat: [1, -2, 1],
        water: -1,

    },
    lajiao: {
        xia: true,
        qiu: true,
        fat: [2, 2, -4],
        water: -1,

    },
    nangua: { // 37.5  沃特49
        qiu: true,
        winter: true,
        fat: [-2, 1, 1],
        water: -2,

    },
    liulian: {  // 25   沃特48
        spring: true,
        fat: [2, -4, 2],
        water: -1,
    }
}

let spring = "";
let xia = "";
let qiu = "";
let winter = "";

for (let key in PLANTS) {
    if(PLANTS[key].spring){ spring += " " + key }
    if(PLANTS[key].xia){ xia += " " + key }
    if(PLANTS[key].qiu){ qiu += " " + key }
    if(PLANTS[key].winter){ winter += " " + key }
}

// console.log("春:"+spring);
// console.log("夏:"+xia);
// console.log("秋:"+qiu);
// console.log("冬:"+winter);

// 春: suan fanqie yumi yangcong luobo potato firelongguo shiliu watergua qiezi luhui liulian
// 夏: suan fanqie yumi yangcong firelongguo shiliu watergua lajiao
// 秋: suan fanqie yumi yangcong luobo potato qiezi lajiao nangua
// 冬: suan luobo potato luhui nangua

// console.log(
//     calcFarm({
//         plants: ["luobo","luobo","luobo","liulian","potato","yumi","yumi","lajiao"]
//     })
// );



let data = analysisPlanta({
    plants: ["luobo","luobo","luobo","liulian","potato","yumi","yumi","lajiao"],
    pei: [],  // suan fanqie yumi yangcong luobo potato qiezi lajiao nangua
    seaon: "spring"
});

fs.writeFileSync("./tmp.json", jsonFormat(data))

function analysisPlanta(props) { // 杂项配对 不考虑巨大化 考虑季节
    let {plants, seaon, pei} = props;
    if(plants.length >= 9) {
        return calcFarm({
            plants: plants,
            seaon: seaon,
        });
    } else {
        let plantsCount = {}
        plants.forEach(v => plantsCount[v] = (plantsCount[v]||0)+1)

        let usePlats;
        if(seaon) {
            usePlats = Object.keys(PLANTS).filter(v => PLANTS[v][seaon]) // PLANTS[v][seaon] &&
        } else {
            usePlats = Object.keys(PLANTS).filter(v => pei.includes(v)) // PLANTS[v][seaon] &&
        }
        let plantInfos = {}
        usePlats.forEach(v => plantInfos[v] = 0)
        plants.forEach(v => plantInfos[v] = (plantInfos[v] || 0) + 1)
        let comb = [];
        if(plants.length == 8) {
            for (let key2 in plantInfos) {
                let pinfo = JSON.parse(JSON.stringify(plantInfos))
                pinfo[key2]++;
                comb.push(pinfo)
            }
        } else if (plants.length == 7) {
            for (let key2 in plantInfos) {
                let pinfo = JSON.parse(JSON.stringify(plantInfos))
                pinfo[key2]++;
                for (let key3 in plantInfos) {
                    let pinfo2 = JSON.parse(JSON.stringify(pinfo))
                    pinfo2[key3]++;
                    comb.push(pinfo2)
                }
            }
        } else {
            comb = getCombination(plantInfos, 9)
            console.log(comb.length);
        }
        comb = comb.map(v => { 
            let plist = [];
            for (let key in v) {
                if(v[key] > 0) {
                    for (let i = 0; i < v[key]; i++) {
                        plist.push(key)
                    }
                }
            }
            v.data = calcFarm({plants:plist});  
            return v;
        });
        comb.sort((b, a) => {return fatPoint(a.data) - fatPoint(b.data)})
        return comb
    }
}

function calcFarm(props) {
    let {plants, seaon} = props
    plants = plants.filter(v => PLANTS[v]);
    let water = 0;
    let fat1 = 0;
    let fat2 = 0;
    let fat3 = 0;

    plants.forEach(item => {
        let plantINfo = PLANTS[item];
        let waterUse = plantINfo.water;
        water += waterUse;
        fat1 += plantINfo.fat[0];
        fat2 += plantINfo.fat[1];
        fat3 += plantINfo.fat[2];
    })

    return {
        water, fat1, fat2, fat3
    }
}







function conb(obj, num) {
    let ret = [];
    let count = num - Object.values(obj).reduce((a,b)=>a+b);
    let entrys = Object.entries(obj);
    if(count >= 0) {
        for (let i = 0; i < count; i++) {
            console.log("test");            
        }
    } else {
        return [obj]
    }
    return ret;
}





function getCombination (obj, max) {
    let arr = Array(max + 1).fill().map((_,i)=>i);
    let entrys = Object.entries(obj)
    let num = entrys.length;
    let value = max - Object.values(obj).reduce((a,b) => a + b)
    var r = [];
    (function f(t, a, n) {
        if (n === 0) {
            if(t.reduce((a1,b1) => a1+b1) !== value){return}
            let ret = {}
            for (let i in t) {
                ret[entrys[i][0]] = t[i]
            }
            for (let key in obj) {
                ret[key] = (ret[key] || 0) + obj[key]
            }
            r.push(ret)
            return ;
        }
        for (var i = 0, l = a.length; i < l; i++) {
            f(t.concat(a[i]), a, n - 1);
        }
    })([], arr, num);
    return r;
}



function fatPoint(data) {
    let {fat1, fat2, fat3} = data;
    let point = 0;
    if(fat1 < 0) { point += fat1/10 }
    if(fat2 < 0) { point += fat2/10 }
    if(fat3 < 0) { point += fat3/10 }
    return point
}